//SPDX-FileCopyrightText: Copyright 2022-2024 深圳市同心圆网络有限公司
//SPDX-License-Identifier: GPL-3.0-only

package user_dao

import (
	"context"

	"atomgit.com/openlinksaas-org/api-server/dao/db_ns"
	"atomgit.com/openlinksaas-org/proto-gen-go.git/user_todo_api"
	"go.mongodb.org/mongo-driver/bson"
	"go.mongodb.org/mongo-driver/mongo"
	"go.mongodb.org/mongo-driver/mongo/options"
)

type BasicTodoInfoDbItem struct {
	Title      string `bson:"title"`
	TitleColor string `bson:"titleColor"`
	Done       bool   `bson:"done"`
}

type TodoInfoDbItem struct {
	TodoId     string              `bson:"_id"`
	UserId     string              `bson:"userId"`
	BasicInfo  BasicTodoInfoDbItem `bson:"basicInfo"`
	CreateTime int64               `bson:"createTime"`
	UpdateTime int64               `bson:"updateTime"`
}

func (item *TodoInfoDbItem) ToTodoInfo() *user_todo_api.TodoInfo {
	return &user_todo_api.TodoInfo{
		TodoId: item.TodoId,
		BasicInfo: &user_todo_api.BasicTodoInfo{
			Title:      item.BasicInfo.Title,
			TitleColor: item.BasicInfo.TitleColor,
			Done:       item.BasicInfo.Done,
		},
		CreateTime: item.CreateTime,
		UpdateTime: item.UpdateTime,
	}
}

type _UserTodoDao struct {
	mc *mongo.Collection
}

func newUserTodoDao(db *mongo.Database) (*_UserTodoDao, error) {
	mc := db.Collection(db_ns.DB_NS_USER + "todo")
	_, err := mc.Indexes().CreateOne(context.Background(), mongo.IndexModel{
		Keys: bson.D{{"userId", 1}, {"updateTime", 1}},
	})
	if err != nil {
		return nil, err
	}
	return &_UserTodoDao{mc: mc}, nil
}

func (db *_UserTodoDao) Insert(ctx context.Context, dbItem *TodoInfoDbItem) error {
	_, err := db.mc.InsertOne(ctx, dbItem)
	return err
}

func (db *_UserTodoDao) Exist(ctx context.Context, userId, todoId string) (bool, error) {
	count, err := db.mc.CountDocuments(ctx, bson.M{"_id": todoId, "userId": userId})
	return count > 0, err
}

func (db *_UserTodoDao) Get(ctx context.Context, userId, todoId string) (*TodoInfoDbItem, error) {
	retItem := &TodoInfoDbItem{}
	err := db.mc.FindOne(ctx, bson.M{"_id": todoId, "userId": userId}).Decode(retItem)
	return retItem, err
}

func (db *_UserTodoDao) Remove(ctx context.Context, userId, todoId string) error {
	_, err := db.mc.DeleteOne(ctx, bson.M{"_id": todoId, "userId": userId})
	return err
}

func (db *_UserTodoDao) RemoveByUser(ctx context.Context, userId string) error {
	_, err := db.mc.DeleteMany(ctx, bson.M{"userId": userId})
	return err
}

func (db *_UserTodoDao) Count(ctx context.Context, userId string, filterByDone, done bool) (uint32, error) {
	query := bson.M{"userId": userId}
	if filterByDone {
		query["basicInfo.done"] = done
	}
	count, err := db.mc.CountDocuments(ctx, query)
	return uint32(count), err
}

func (db *_UserTodoDao) List(ctx context.Context, userId string) ([]*TodoInfoDbItem, error) {
	query := bson.M{"userId": userId}
	cursor, err := db.mc.Find(ctx, query, options.Find().SetSort(bson.M{"updateTime": -1}))
	if err != nil {
		return nil, err
	}
	retList := []*TodoInfoDbItem{}
	err = cursor.All(ctx, &retList)
	return retList, err
}

func (db *_UserTodoDao) Update(ctx context.Context, userId, todoId string, basicDbItem *BasicTodoInfoDbItem, updateTime int64) error {
	_, err := db.mc.UpdateOne(ctx, bson.M{"_id": todoId, "userId": userId}, bson.M{
		"$set": bson.M{
			"basicInfo":  basicDbItem,
			"updateTime": updateTime,
		},
	})
	return err
}
